package com.wl156.adapters;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wl156.lite.R;

public class TruckAdapter extends CheckableAdapter implements OnClickListener {

	public TruckAdapter(Context context, SoapObject data) {
		this(context, data, DEFAULT);
	}

	public TruckAdapter(Context context, SoapObject data, int type) {
		mInflater = LayoutInflater.from(context);
		mData = data;
		mContext = context;
		mType = type;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_truck, null);
		}

		TextView carCode = (TextView) convertView.findViewById(R.id.car_code);
		TextView place = (TextView) convertView.findViewById(R.id.place);
		TextView driverName = (TextView) convertView
				.findViewById(R.id.driver_name);
		TextView ownerName = (TextView) convertView
				.findViewById(R.id.owner_name);

		TextView truckType = (TextView) convertView
				.findViewById(R.id.truck_type);
		TextView tonnage = (TextView) convertView.findViewById(R.id.tonnage);
		TextView length = (TextView) convertView.findViewById(R.id.length);
		TextView creditScore = (TextView) convertView
				.findViewById(R.id.credit_score);

		TextView phoneDial = (TextView) convertView.findViewById(R.id.dial);
		phoneDial.setOnClickListener(this);

		int phoneIndex = 0;
		switch (mType) {
		case DEFAULT:
			setTextViewStringFromProperty(position, carCode, 1);
			setTextViewStringFromProperty(position, place, 2);
			setTextViewStringFromProperty(position, driverName, 3);
			setTextViewStringFromProperty(position, ownerName, 4);
			setTextViewStringFromProperty(position, truckType, 5);
			setTextViewStringFromProperty(position, tonnage, 6);
			setTextViewStringFromProperty(position, length, 7);
			setTextViewStringFromProperty(position, creditScore, 10);
			phoneIndex = 8;
			break;
		case COLLECT:
			setTextViewStringFromProperty(position, carCode, 2);
			setTextViewStringFromProperty(position, place, 3);
			setTextViewStringFromProperty(position, driverName, 4);
			setTextViewStringFromProperty(position, ownerName, 5);
			setTextViewStringFromProperty(position, truckType, 6);
			setTextViewStringFromProperty(position, tonnage, 7);
			setTextViewStringFromProperty(position, length, 8);
			setTextViewStringFromProperty(position, creditScore, 11);
			phoneIndex = 9;
			break;
		}

		setPhoneView(position, phoneDial, phoneIndex);
		super.getView(position, convertView, parent);
		return convertView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dial:
			Intent i = new Intent(Intent.ACTION_CALL);
			i.setData(Uri.parse("tel:" + v.getTag().toString()));
			mContext.startActivity(i);
			break;
		}
	}
}
